package org.stimuliband.microphone;

import org.apache.log4j.Logger;

/**
 *
 * @author
 */
public class HighVolumeListener implements MicrophoneListener {

    static Logger logger = Logger.getLogger(HighVolumeListener.class.getSimpleName());
    final int threshold;

    public HighVolumeListener(int threshold) {
        this.threshold = threshold;
    }

    @Override
    public void onCaptured(byte[] captured) {
        int maxAmplitude = 0;
        for (int i = 0; i < captured.length; i++) {
            int ampl = Math.abs(((short) captured[i]));
            if (ampl > maxAmplitude) {
                maxAmplitude = ampl;
            }
        }
        if (maxAmplitude > threshold) {
            logger.debug("Max amplitude " + maxAmplitude + " exceeded threshold " + threshold + ".");
        }
    }
}
